﻿package Classes.Enemies.Boss{
	import flash.events.Event;	
	import flash.display.Sprite;
	
	import Classes.Enemies.*;
	import Classes.Elements.*;
	import Classes.Utils.*;
	import Classes.Weapons.*;
	import Classes.Weapons.Bullets.*;
	import Classes.Weapons.Rockets.*;
	import Classes.Interface.*;
	import Classes.Sounds.GameSound;
	import Classes.Explode.*;

	public class Osprey extends Aircraft implements ILaunch{
	
		public var tmpx:Number,tmpy:Number;
		private var radius:Number;
		private var crtr:Number;
		public var rocketType:Object;
		private var numGuns:int;
		
		public function Osprey(){						
			speed = 2;
			life = 3000;
			mcLifeBar.max = life;
			hp = life;
			execution = 40;
			crtspeed = 0;
			accelerate = .5;
			condition = 110;
			score = UnitType.Osprey.score;
			fireable  = false;
			radius 		= 100;
			crtr		= 1;
			numGuns 	= 2;
			weaponType 	= WeaponType.PhotonBullet;
			rocketType 	= WeaponType.Bazzoka;
		}

		override public function fire():void{
			if(inc % weaponType.interval == 0){
				for(var i:int=0;i<12;i++){
					var photon:PhotonBullet = new PhotonBullet();
					photon.aTargets = this.aTargets;
					photon.sender = this;
					photon.x = this.x;
					photon.y = this.y;
					photon.rotation = 30*i;
					photon.angle = photon.rotation;
					bulletLayer.addChild(photon);				
				}
			}
		}
		
		public function launch():void{
			if(WeaponType != WeaponType.Nothing){
				if(inc%(rocketType.interval/2)==0){
					createMissile();				
				}
			}
		}		
		
		private function createMissile():void{
			for(var i:int=0;i<numGuns;i++){
				var bazzoka:Bazzoka = new Bazzoka();
				bazzoka.moverange = moverange;
				bazzoka.aTargets = this.aTargets;		
				bazzoka.dimension = dimension;
				bazzoka.x = this.x -(i- numGuns/2 + .5) * 12;
				bazzoka.y = this.y;	
				if(aTargets.length){
					Moving.pointTo(bazzoka,aTargets[0]);
				}
				bazzoka.angle = -bazzoka.rotation;
				dimension.addChildAt(bazzoka,0);
				aFriends.push(bazzoka);
			}
		}
		
		override protected function move():void{
			if(!aTargets.length)
				fireable = false;
			if(fireable){
				fire();
				launch();
			}
			if(!this.moveable)
				return;
			if(this.y<condition && inc==0){
				this.y+= this.speed;
				tmpx = this.x;
				tmpy = this.y;
				gotoAndStop("Normal");
			}			
			else{
				fireable = true;
				crtr = inc==1?1:crtr;
				if(crtr<radius)
					crtr += accelerate;
				this.hstate = Moving.moveCircle(this,speed,crtr,inc*1.5,tmpx,condition);
				if(hstate==-1)
					gotoAndStop("MoveLeft");
				else if(hstate==1)						
					gotoAndStop("MoveRight");
				else
					gotoAndStop("Normal");
				inc++;			
			}
			hitTest();
		}
		
		override public function set hp(value:Number):void{
			_hp = value;
			mcLifeBar.value = _hp;
			if(_hp<=0){
				var ex:Explode = new BossExplode();
				ex.x = this.x;
				ex.y = this.y;
				parent.addChild(ex);
			}
			live();
		}
		override public function get hp():Number	{
			return _hp;
		}
	}
}